﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Register.IDAL;
using System.Data.SqlClient;
using System.Data;
using Register.Model;
using Register.DBUtility;

namespace Register.DAL
{
    /// <summary>
    /// FileName: ModuleInfoDAL.cs
    /// CLRVersion: 4.0.30319.269
    /// Author: WangYan
    /// Corporation: 
    /// Description: 
    /// DateTime: 2012/6/24 19:24:24
    /// </summary>
    public class ModuleInfoDAL : IModuleInfoDAL
    {
        public List<Model.ModuleInfo> GetModuleInfoByTopID(string moduleTopID)
        {
            String sql = " select  ModuleInfo_ID,ModuleInfo_Page,ModuleInfo_Name,ModuleTop_ID from System_moduleInfo where ModuleTop_ID=@ID ";
            SqlParameter[] paramters = new SqlParameter[] { 
            new SqlParameter("@ID",SqlDbType.VarChar)
            };
            paramters[0].Value=moduleTopID;
            return DataSetToList(DbHelperSQL.Query(sql,paramters));
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ModuleInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into System_moduleInfo(");
            strSql.Append("ModuleInfo_ID,ModuleTop_ID,ModuleInfo_Page,ModuleInfo_Name,ModuleInfo_Describe,ModuleInfo_AddTime,ModuleInfo_Recoder,ModuleInfo_LastAmmendTime,ModuleInfo_LastAmmendPerson)");
            strSql.Append(" values (");
            strSql.Append("@ModuleInfo_ID,@ModuleTop_ID,@ModuleInfo_Page,@ModuleInfo_Name,@ModuleInfo_Describe,@ModuleInfo_AddTime,@ModuleInfo_Recoder,@ModuleInfo_LastAmmendTime,@ModuleInfo_LastAmmendPerson)");
            SqlParameter[] parameters = {
					new SqlParameter("@ModuleInfo_ID", SqlDbType.Char,5),
					new SqlParameter("@ModuleTop_ID", SqlDbType.Char,5),
					new SqlParameter("@ModuleInfo_Page", SqlDbType.VarChar,20),
					new SqlParameter("@ModuleInfo_Name", SqlDbType.VarChar,20),
					new SqlParameter("@ModuleInfo_Describe", SqlDbType.VarChar,200),
					new SqlParameter("@ModuleInfo_AddTime", SqlDbType.DateTime),
					new SqlParameter("@ModuleInfo_Recoder", SqlDbType.VarChar,20),
					new SqlParameter("@ModuleInfo_LastAmmendTime", SqlDbType.DateTime),
					new SqlParameter("@ModuleInfo_LastAmmendPerson", SqlDbType.VarChar,20)};
            parameters[0].Value = model.ModuleInfo_ID;
            parameters[1].Value = model.ModuleTop_ID;
            parameters[2].Value = model.ModuleInfo_Page;
            parameters[3].Value = model.ModuleInfo_Name;
            parameters[4].Value = model.ModuleInfo_Describe;
            parameters[5].Value = model.ModuleInfo_AddTime;
            parameters[6].Value = model.ModuleInfo_Recoder;
            parameters[7].Value = model.ModuleInfo_LastAmmendTime;
            parameters[8].Value = model.ModuleInfo_LastAmmendPerson;

     return       DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ModuleInfo_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from System_moduleInfo ");
            strSql.Append(" where ModuleInfo_ID=@ModuleInfo_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ModuleInfo_ID", SqlDbType.Char,5)};
            parameters[0].Value = ModuleInfo_ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        // <summary>
        /// 获得数据列表
        /// </summary>
        public List<ModuleInfo> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ModuleInfo_ID,ModuleTop_ID,ModuleInfo_Page,ModuleInfo_Name,ModuleInfo_Describe,ModuleInfo_AddTime,ModuleInfo_Recoder,ModuleInfo_LastAmmendTime,ModuleInfo_LastAmmendPerson ");
            strSql.Append(" FROM System_moduleInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataSetToList(DbHelperSQL.Query(strSql.ToString()));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        private List<ModuleInfo> DataSetToList(DataSet ds)
        {
            List<ModuleInfo> list = new List<ModuleInfo>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                ModuleInfo mi = new ModuleInfo();
                mi.ModuleInfo_ID = (String)row["ModuleInfo_ID"];
                mi.ModuleInfo_Name = (String)row["ModuleInfo_Name"];
                mi.ModuleInfo_Page = (String)row["ModuleInfo_Page"];
                mi.ModuleTop_ID = (String)row["ModuleTop_ID"];
                list.Add(mi);
            }
            return list;
        }
    }
}
